//
//  ViewController.m
//  10803-PickerView
//
//  Created by student on 16/2/26.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "ProvinceModel.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic,strong) NSArray *ProData;

@property (nonatomic,strong) NSArray *cityData;
@end


@implementation ViewController



-(NSArray *)ProData{
    if (_ProData == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"area1.plist" ofType:nil];
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *mutArr = [NSMutableArray array];
        
        for (NSDictionary *dic in tempArr) {
            ProvinceModel *pro = [ProvinceModel ProvinceModelWithDict:dic];
            [mutArr addObject:pro];
        }
        _ProData = mutArr;
    }
    return _ProData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

//- (NSInteger)selectedRowInComponent:(NSInteger)component{
//    
//}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.ProData.count;
    }
    else{
        NSInteger curRow = [self.pickerView selectedRowInComponent:0];
        ProvinceModel *pro = self.ProData[curRow];
        return pro.Cities.count;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        ProvinceModel *pro = self.ProData[row];
        return pro.State;
    }
    else{
        NSInteger curRow = [self.pickerView selectedRowInComponent:0];
        ProvinceModel *pro = self.ProData[curRow];
        _cityData = pro.Cities;
        return self.cityData[row][@"city"];
        }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        ProvinceModel *promodel = self.ProData[row];
        self.cityData = promodel.Cities;
        [self.pickerView reloadComponent:1];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
